home *** CD-ROM | disk | FTP | other *** search
/ Clickx 23 / Clickx 23.iso / DATA / wordpress / wp-includes / functions.php < prev    next >
Encoding:
PHP Script  |  2005-05-27  |  50.5 KB  |  1,852 lines

  1. <?php
  2.  
  3. require_once(dirname(__FILE__).'/functions-compat.php');
  4.  
  5. if (!function_exists('_')) {
  6.     function _($string) {
  7.         return $string;
  8.     }
  9. }
  10.  
  11. function get_profile($field, $user = false) {
  12.     global $wpdb;
  13.     if (!$user)
  14.         $user = $wpdb->escape($_COOKIE['wordpressuser_' . COOKIEHASH]);
  15.     return $wpdb->get_var("SELECT $field FROM $wpdb->users WHERE user_login = '$user'");
  16. }
  17.  
  18. function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
  19.     global $month, $weekday, $month_abbrev, $weekday_abbrev;
  20.     $m = $mysqlstring;
  21.     if (empty($m)) {
  22.         return false;
  23.     }
  24.     $i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4)); 
  25.     if (!empty($month) && !empty($weekday) && $translate) {
  26.         $datemonth = $month[date('m', $i)];
  27.         $datemonth_abbrev = $month_abbrev[$datemonth];
  28.         $dateweekday = $weekday[date('w', $i)];
  29.         $dateweekday_abbrev = $weekday_abbrev[$dateweekday];         
  30.         $dateformatstring = ' '.$dateformatstring;
  31.         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
  32.         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
  33.         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
  34.         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
  35.     
  36.         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
  37.     }
  38.     $j = @date($dateformatstring, $i);
  39.     if (!$j) {
  40.     // for debug purposes
  41.     //    echo $i." ".$mysqlstring;
  42.     }
  43.     return $j;
  44. }
  45.  
  46. function current_time($type, $gmt = 0) {
  47.     switch ($type) {
  48.         case 'mysql':
  49.             if ($gmt) $d = gmdate('Y-m-d H:i:s');
  50.             else $d = gmdate('Y-m-d H:i:s', (time() + (get_settings('gmt_offset') * 3600)));
  51.             return $d;
  52.             break;
  53.         case 'timestamp':
  54.             if ($gmt) $d = time();
  55.             else $d = time() + (get_settings('gmt_offset') * 3600);
  56.             return $d;
  57.             break;
  58.     }
  59. }
  60.  
  61. function date_i18n($dateformatstring, $unixtimestamp) {
  62.     global $month, $weekday, $month_abbrev, $weekday_abbrev;
  63.     $i = $unixtimestamp; 
  64.     if ((!empty($month)) && (!empty($weekday))) {
  65.         $datemonth = $month[date('m', $i)];
  66.         $datemonth_abbrev = $month_abbrev[$datemonth];
  67.         $dateweekday = $weekday[date('w', $i)];
  68.         $dateweekday_abbrev = $weekday_abbrev[$dateweekday];         
  69.         $dateformatstring = ' '.$dateformatstring;
  70.         $dateformatstring = preg_replace("/([^\\\])D/", "\\1".backslashit($dateweekday_abbrev), $dateformatstring);
  71.         $dateformatstring = preg_replace("/([^\\\])F/", "\\1".backslashit($datemonth), $dateformatstring);
  72.         $dateformatstring = preg_replace("/([^\\\])l/", "\\1".backslashit($dateweekday), $dateformatstring);
  73.         $dateformatstring = preg_replace("/([^\\\])M/", "\\1".backslashit($datemonth_abbrev), $dateformatstring);
  74.         $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
  75.     }
  76.     $j = @date($dateformatstring, $i);
  77.     return $j;
  78.     }
  79.  
  80. function get_weekstartend($mysqlstring, $start_of_week) {
  81.     $my = substr($mysqlstring,0,4);
  82.     $mm = substr($mysqlstring,8,2);
  83.     $md = substr($mysqlstring,5,2);
  84.     $day = mktime(0,0,0, $md, $mm, $my);
  85.     $weekday = date('w',$day);
  86.     $i = 86400;
  87.  
  88.     if ($weekday < get_settings('start_of_week'))
  89.         $weekday = 7 - (get_settings('start_of_week') - $weekday);
  90.  
  91.     while ($weekday > get_settings('start_of_week')) {
  92.         $weekday = date('w',$day);
  93.         if ($weekday < get_settings('start_of_week'))
  94.             $weekday = 7 - (get_settings('start_of_week') - $weekday);
  95.  
  96.         $day = $day - 86400;
  97.         $i = 0;
  98.     }
  99.     $week['start'] = $day + 86400 - $i;
  100.     //$week['end']   = $day - $i + 691199;
  101.     $week['end'] = $week['start'] + 604799;
  102.     return $week;
  103. }
  104.  
  105. function get_lastpostdate($timezone = 'server') {
  106.     global $cache_lastpostdate, $pagenow, $wpdb;
  107.     $add_seconds_blog = get_settings('gmt_offset') * 3600;
  108.     $add_seconds_server = date('Z');
  109.     $now = current_time('mysql', 1);
  110.     if ( !isset($cache_lastpostdate[$timezone]) ) {
  111.         switch(strtolower($timezone)) {
  112.             case 'gmt':
  113.                 $lastpostdate = $wpdb->get_var("SELECT post_date_gmt FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
  114.                 break;
  115.             case 'blog':
  116.                 $lastpostdate = $wpdb->get_var("SELECT post_date FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
  117.                 break;
  118.             case 'server':
  119.                 $lastpostdate = $wpdb->get_var("SELECT DATE_ADD(post_date_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_date_gmt <= '$now' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1");
  120.                 break;
  121.         }
  122.         $cache_lastpostdate[$timezone] = $lastpostdate;
  123.     } else {
  124.         $lastpostdate = $cache_lastpostdate[$timezone];
  125.     }
  126.     return $lastpostdate;
  127. }
  128.  
  129. function get_lastpostmodified($timezone = 'server') {
  130.     global $cache_lastpostmodified, $pagenow, $wpdb;
  131.     $add_seconds_blog = get_settings('gmt_offset') * 3600;
  132.     $add_seconds_server = date('Z');
  133.     $now = current_time('mysql', 1);
  134.     if ( !isset($cache_lastpostmodified[$timezone]) ) {
  135.         switch(strtolower($timezone)) {
  136.             case 'gmt':
  137.                 $lastpostmodified = $wpdb->get_var("SELECT post_modified_gmt FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
  138.                 break;
  139.             case 'blog':
  140.                 $lastpostmodified = $wpdb->get_var("SELECT post_modified FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
  141.                 break;
  142.             case 'server':
  143.                 $lastpostmodified = $wpdb->get_var("SELECT DATE_ADD(post_modified_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_modified_gmt <= '$now' AND post_status = 'publish' ORDER BY post_modified_gmt DESC LIMIT 1");
  144.                 break;
  145.         }
  146.         $lastpostdate = get_lastpostdate($timezone);
  147.         if ($lastpostdate > $lastpostmodified) {
  148.             $lastpostmodified = $lastpostdate;
  149.         }
  150.         $cache_lastpostmodified[$timezone] = $lastpostmodified;
  151.     } else {
  152.         $lastpostmodified = $cache_lastpostmodified[$timezone];
  153.     }
  154.     return $lastpostmodified;
  155. }
  156.  
  157. function user_pass_ok($user_login,$user_pass) {
  158.     global $cache_userdata;
  159.     if ( empty($cache_userdata[$user_login]) ) {
  160.         $userdata = get_userdatabylogin($user_login);
  161.     } else {
  162.         $userdata = $cache_userdata[$user_login];
  163.     }
  164.     return (md5($user_pass) == $userdata->user_pass);
  165. }
  166.  
  167.  
  168. function get_usernumposts($userid) {
  169.     global $wpdb;
  170.     return $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = '$userid' AND post_status = 'publish'");
  171. }
  172.  
  173. // examine a url (supposedly from this blog) and try to
  174. // determine the post ID it represents.
  175. function url_to_postid($url) {
  176.     global $wp_rewrite;
  177.  
  178.     // First, check to see if there is a 'p=N' or 'page_id=N' to match against:
  179.     preg_match('#[?&](p|page_id)=(\d+)#', $url, $values);
  180.     $id = intval($values[2]);
  181.     if ($id) return $id;
  182.  
  183.     // URI is probably a permalink.
  184.     $rewrite = $wp_rewrite->wp_rewrite_rules();
  185.  
  186.     if ( empty($rewrite) )
  187.         return 0;
  188.  
  189.     $req_uri = $url;
  190.  
  191.     if ( false !== strpos($req_uri, get_settings('home')) ) {
  192.         $req_uri = str_replace(get_settings('home'), '', $req_uri);
  193.     } else {
  194.         $home_path = parse_url(get_settings('home'));
  195.         $home_path = $home_path['path'];
  196.         $req_uri = str_replace($home_path, '', $req_uri);
  197.     }
  198.  
  199.     $req_uri = trim($req_uri, '/');
  200.     $request = $req_uri;
  201.     
  202.     // Look for matches.
  203.     $request_match = $request;
  204.     foreach ($rewrite as $match => $query) {
  205.         // If the requesting file is the anchor of the match, prepend it
  206.         // to the path info.
  207.         if ((! empty($req_uri)) && (strpos($match, $req_uri) === 0)) {
  208.             $request_match = $req_uri . '/' . $request;
  209.         }
  210.  
  211.         if (preg_match("!^$match!", $request_match, $matches)) {
  212.             // Got a match.
  213.             // Trim the query of everything up to the '?'.
  214.             $query = preg_replace("!^.+\?!", '', $query);
  215.             
  216.             // Substitute the substring matches into the query.
  217.             eval("\$query = \"$query\";");
  218.             $query = new WP_Query($query);
  219.             if ( !empty($query->post) )
  220.                 return $query->post->ID;
  221.             else
  222.                 return 0;
  223.         }
  224.     }
  225.  
  226.     return 0;
  227. }
  228.  
  229.  
  230. /* Options functions */
  231.  
  232. function get_settings($setting) {
  233.   global $wpdb, $cache_settings, $cache_nonexistantoptions;
  234.     if ( strstr($_SERVER['REQUEST_URI'], 'wp-admin/install.php') || defined('WP_INSTALLING') )
  235.         return false;
  236.  
  237.     if ( empty($cache_settings) )
  238.         $cache_settings = get_alloptions();
  239.  
  240.     if ( empty($cache_nonexistantoptions) )
  241.         $cache_nonexistantoptions = array();
  242.  
  243.     if ('home' == $setting && '' == $cache_settings->home)
  244.         return apply_filters('option_' . $setting, $cache_settings->siteurl);
  245.  
  246.     if ( isset($cache_settings->$setting) ) :
  247.         return apply_filters('option_' . $setting, $cache_settings->$setting);
  248.     else :
  249.         // for these cases when we're asking for an unknown option
  250.         if ( isset($cache_nonexistantoptions[$setting]) )
  251.             return false;
  252.  
  253.         $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
  254.  
  255.         if (!$option) :
  256.             $cache_nonexistantoptions[$setting] = true;
  257.             return false;
  258.         endif;
  259.  
  260.         @ $kellogs = unserialize($option);
  261.         if ($kellogs !== FALSE)
  262.             return apply_filters('option_' . $setting, $kellogs);
  263.         else return apply_filters('option_' . $setting, $option);
  264.     endif;
  265. }
  266.  
  267. function get_option($option) {
  268.     return get_settings($option);
  269. }
  270.  
  271. function form_option($option) {
  272.     echo htmlspecialchars( get_option($option), ENT_QUOTES );
  273. }
  274.  
  275. function get_alloptions() {
  276.     global $wpdb, $wp_queries;
  277.     $wpdb->hide_errors();
  278.     if (!$options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'")) {
  279.         $options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options");
  280.     }
  281.     $wpdb->show_errors();
  282.  
  283.     foreach ($options as $option) {
  284.         // "When trying to design a foolproof system, 
  285.         //  never underestimate the ingenuity of the fools :)" -- Dougal
  286.         if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  287.         if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  288.         if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
  289.         @ $value = unserialize($option->option_value);
  290.         if ($value === FALSE)
  291.             $value = $option->option_value;
  292.         $all_options->{$option->option_name} = apply_filters('pre_option_' . $option->option_name, $value);
  293.     }
  294.     return apply_filters('all_options', $all_options);
  295. }
  296.  
  297. function update_option($option_name, $newvalue) {
  298.     global $wpdb, $cache_settings;
  299.     if ( is_array($newvalue) || is_object($newvalue) )
  300.         $newvalue = serialize($newvalue);
  301.  
  302.     $newvalue = trim($newvalue); // I can't think of any situation we wouldn't want to trim
  303.  
  304.     // If the new and old values are the same, no need to update.
  305.     if ($newvalue == get_option($option_name)) {
  306.         return true;
  307.     }
  308.  
  309.     // If it's not there add it
  310.     if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$option_name'") )
  311.         add_option($option_name);
  312.  
  313.     $newvalue = $wpdb->escape($newvalue);
  314.     $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
  315.     $cache_settings = get_alloptions(); // Re cache settings
  316.     return true;
  317. }
  318.  
  319.  
  320. // thx Alex Stapleton, http://alex.vort-x.net/blog/
  321. function add_option($name, $value = '', $description = '', $autoload = 'yes') {
  322.     global $wpdb;
  323.     $original = $value;
  324.     if ( is_array($value) || is_object($value) )
  325.         $value = serialize($value);
  326.  
  327.     if( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$name'") ) {
  328.         $name = $wpdb->escape($name);
  329.         $value = $wpdb->escape($value);
  330.         $description = $wpdb->escape($description);
  331.         $wpdb->query("INSERT INTO $wpdb->options (option_name, option_value, option_description, autoload) VALUES ('$name', '$value', '$description', '$autoload')");
  332.  
  333.         if($wpdb->insert_id) {
  334.             global $cache_settings;
  335.             $cache_settings->{$name} = $original;
  336.         }
  337.     }
  338.     return;
  339. }
  340.  
  341. function delete_option($name) {
  342.     global $wpdb;
  343.     // Get the ID, if no ID then return
  344.     $option_id = $wpdb->get_var("SELECT option_id FROM $wpdb->options WHERE option_name = '$name'");
  345.     if (!$option_id) return false;
  346.     $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = '$name'");
  347.     return true;
  348. }
  349.  
  350. function add_post_meta($post_id, $key, $value, $unique = false) {
  351.     global $wpdb;
  352.     
  353.     if ($unique) {
  354.         if( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
  355. = '$key' AND post_id = '$post_id'") ) {
  356.             return false;
  357.         }
  358.     }
  359.  
  360.     $wpdb->query("INSERT INTO $wpdb->postmeta
  361.                                 (post_id,meta_key,meta_value) 
  362.                                 VALUES ('$post_id','$key','$value')
  363.                         ");
  364.     
  365.     return true;
  366. }
  367.  
  368. function delete_post_meta($post_id, $key, $value = '') {
  369.     global $wpdb;
  370.  
  371.     if (empty($value)) {
  372.         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
  373. post_id = '$post_id' AND meta_key = '$key'");
  374.     } else {
  375.         $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
  376. post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
  377.     }
  378.  
  379.     if (!$meta_id) return false;
  380.  
  381.     if (empty($value)) {
  382.         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
  383. AND meta_key = '$key'");
  384.     } else {
  385.         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
  386. AND meta_key = '$key' AND meta_value = '$value'");
  387.     }
  388.         
  389.     return true;
  390. }
  391.  
  392. function get_post_meta($post_id, $key, $single = false) {
  393.     global $wpdb, $post_meta_cache;
  394.  
  395.     if (isset($post_meta_cache[$post_id][$key])) {
  396.         if ($single) {
  397.             return $post_meta_cache[$post_id][$key][0];
  398.         } else {
  399.             return $post_meta_cache[$post_id][$key];
  400.         }
  401.     }
  402.  
  403.     $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
  404.  
  405.     $values = array();
  406.     if ($metalist) {
  407.         foreach ($metalist as $metarow) {
  408.             $values[] = $metarow[0];
  409.         }
  410.     }
  411.  
  412.     if ($single) {
  413.         if (count($values)) {
  414.             return $values[0];
  415.         } else {
  416.             return '';
  417.         }
  418.     } else {
  419.         return $values;
  420.     }
  421. }
  422.  
  423. function update_post_meta($post_id, $key, $value, $prev_value = '') {
  424.     global $wpdb, $post_meta_cache;
  425.  
  426.         if(! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
  427. = '$key' AND post_id = '$post_id'") ) {
  428.             return false;
  429.         }
  430.  
  431.     if (empty($prev_value)) {
  432.         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
  433. meta_key = '$key' AND post_id = '$post_id'");
  434.     } else {
  435.         $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
  436. meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
  437.     }
  438.  
  439.     return true;
  440. }
  441.  
  442. // Deprecated.  Use get_post().
  443. function get_postdata($postid) {
  444.     $post = &get_post($postid);
  445.     
  446.     $postdata = array (
  447.         'ID' => $post->ID, 
  448.         'Author_ID' => $post->post_author, 
  449.         'Date' => $post->post_date, 
  450.         'Content' => $post->post_content, 
  451.         'Excerpt' => $post->post_excerpt, 
  452.         'Title' => $post->post_title, 
  453.         'Category' => $post->post_category,
  454.         'post_status' => $post->post_status,
  455.         'comment_status' => $post->comment_status,
  456.         'ping_status' => $post->ping_status,
  457.         'post_password' => $post->post_password,
  458.         'to_ping' => $post->to_ping,
  459.         'pinged' => $post->pinged,
  460.         'post_name' => $post->post_name
  461.     );
  462.  
  463.     return $postdata;
  464. }
  465.  
  466. // Retrieves post data given a post ID or post object. 
  467. // Handles post caching.
  468. function &get_post(&$post, $output = OBJECT) {
  469.     global $post_cache, $wpdb;
  470.  
  471.     if ( empty($post) ) {
  472.         if ( isset($GLOBALS['post']) )
  473.             $post = & $GLOBALS['post'];
  474.         else
  475.             $post = null;
  476.     } elseif (is_object($post) ) {
  477.         if (! isset($post_cache[$post->ID]))
  478.             $post_cache[$post->ID] = &$post;
  479.         $post = & $post_cache[$post->ID];
  480.     } else {
  481.         if (isset($post_cache[$post]))
  482.             $post = & $post_cache[$post];
  483.         else {
  484.             $query = "SELECT * FROM $wpdb->posts WHERE ID=$post";
  485.             $post_cache[$post] = & $wpdb->get_row($query);
  486.             $post = & $post_cache[$post];
  487.         }
  488.     }
  489.  
  490.     if ( $output == OBJECT ) {
  491.         return $post;
  492.     } elseif ( $output == ARRAY_A ) {
  493.         return get_object_vars($post);
  494.     } elseif ( $output == ARRAY_N ) {
  495.         return array_values(get_object_vars($post));
  496.     } else {
  497.         return $post;
  498.     }
  499. }
  500.  
  501. // Retrieves page data given a page ID or page object. 
  502. // Handles page caching.
  503. function &get_page(&$page, $output = OBJECT) {
  504.     global $page_cache, $wpdb;
  505.  
  506.     if ( empty($page) ) {
  507.         if ( isset($GLOBALS['page']) )
  508.             $page = & $GLOBALS['page'];
  509.         else
  510.             $page = null;
  511.     } elseif (is_object($page) ) {
  512.         if (! isset($page_cache[$page->ID]))
  513.             $page_cache[$page->ID] = &$page;
  514.         $page = & $page_cache[$page->ID];
  515.     } else {
  516.         if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) )
  517.             $page = & $GLOBALS['page'];
  518.         elseif (isset($page_cache[$page]))
  519.             $page = & $page_cache[$page];
  520.         else {
  521.             $query = "SELECT * FROM $wpdb->posts WHERE ID=$page";
  522.             $page_cache[$page] = & $wpdb->get_row($query);
  523.             $page = & $page_cache[$page];
  524.         }
  525.     }
  526.  
  527.     if ( $output == OBJECT ) {
  528.         return $page;
  529.     } elseif ( $output == ARRAY_A ) {
  530.         return get_object_vars($page);
  531.     } elseif ( $output == ARRAY_N ) {
  532.         return array_values(get_object_vars($page));
  533.     } else {
  534.         return $page;
  535.     }
  536. }
  537.  
  538. // Retrieves category data given a category ID or category object. 
  539. // Handles category caching.
  540. function &get_category(&$category, $output = OBJECT) {
  541.     global $cache_categories, $wpdb;
  542.  
  543.     if ( empty($category) )
  544.         return null;
  545.  
  546.     $category = (int) $category;
  547.  
  548.     if ( ! isset($cache_categories))
  549.         update_category_cache();
  550.  
  551.     if (is_object($category)) {
  552.         if ( ! isset($cache_categories[$category->cat_ID]))
  553.             $cache_categories[$category->cat_ID] = &$category;
  554.         $category = & $cache_categories[$category->cat_ID];
  555.     } else {
  556.         if ( !isset($cache_categories[$category]) ) {
  557.             $category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = $category");
  558.             $cache_categories[$category->cat_ID] = & $category;
  559.         } else {
  560.             $category = & $cache_categories[$category];
  561.         }
  562.     }
  563.  
  564.     if ( $output == OBJECT ) {
  565.         return $category;
  566.     } elseif ( $output == ARRAY_A ) {
  567.         return get_object_vars($category);
  568.     } elseif ( $output == ARRAY_N ) {
  569.         return array_values(get_object_vars($category));
  570.     } else {
  571.         return $category;
  572.     }
  573. }
  574.  
  575. function get_catname($cat_ID) {
  576.     $category = &get_category($cat_ID);
  577.     return $category->cat_name;
  578. }
  579.  
  580. function gzip_compression() {
  581.     if ( strstr($_SERVER['PHP_SELF'], 'wp-admin') ) return false;
  582.     if ( !get_settings('gzipcompression') ) return false;
  583.  
  584.     if( extension_loaded('zlib') ) {
  585.         ob_start('ob_gzhandler');
  586.     }
  587. }
  588.  
  589.  
  590. // functions to count the page generation time (from phpBB2)
  591. // ( or just any time between timer_start() and timer_stop() )
  592.  
  593. function timer_stop($display = 0, $precision = 3) { //if called like timer_stop(1), will echo $timetotal
  594.     global $timestart, $timeend;
  595.     $mtime = microtime();
  596.     $mtime = explode(' ',$mtime);
  597.     $mtime = $mtime[1] + $mtime[0];
  598.     $timeend = $mtime;
  599.     $timetotal = $timeend-$timestart;
  600.     if ($display)
  601.         echo number_format($timetotal,$precision);
  602.     return $timetotal;
  603. }
  604.  
  605. function weblog_ping($server = '', $path = '') {
  606.     global $wp_version;
  607.     include_once (ABSPATH . WPINC . '/class-IXR.php');
  608.  
  609.     // using a timeout of 3 seconds should be enough to cover slow servers
  610.     $client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
  611.     $client->timeout = 3;
  612.     $client->useragent .= ' -- WordPress/'.$wp_version;
  613.  
  614.     // when set to true, this outputs debug messages by itself
  615.     $client->debug = false;
  616.     $home = trailingslashit( get_option('home') );
  617.     if ( !$client->query('weblogUpdates.extendedPing', get_settings('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
  618.         $client->query('weblogUpdates.ping', get_settings('blogname'), $home);
  619. }
  620.  
  621. function generic_ping($post_id = 0) {
  622.     $services = get_settings('ping_sites');
  623.     $services = preg_replace("|(\s)+|", '$1', $services); // Kill dupe lines
  624.     $services = trim($services);
  625.     if ('' != $services) {
  626.         $services = explode("\n", $services);
  627.         foreach ($services as $service) {
  628.             weblog_ping($service);
  629.         }
  630.     }
  631.  
  632.     return $post_id;
  633. }
  634.  
  635. // Send a Trackback
  636. function trackback($trackback_url, $title, $excerpt, $ID) {
  637.     global $wpdb, $wp_version;
  638.  
  639.     if (empty($trackback_url))
  640.         return;
  641.  
  642.     $title = urlencode($title);
  643.     $excerpt = urlencode($excerpt);
  644.     $blog_name = urlencode(get_settings('blogname'));
  645.     $tb_url = $trackback_url;
  646.     $url = urlencode(get_permalink($ID));
  647.     $query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
  648.     $trackback_url = parse_url($trackback_url);
  649.     $http_request  = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
  650.     $http_request .= 'Host: '.$trackback_url['host']."\r\n";
  651.     $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_settings('blog_charset')."\r\n";
  652.     $http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
  653.     $http_request .= "User-Agent: WordPress/" . $wp_version;
  654.     $http_request .= "\r\n\r\n";
  655.     $http_request .= $query_string;
  656.     if ( '' == $trackback_url['port'] )
  657.         $trackback_url['port'] = 80;
  658.     $fs = @fsockopen($trackback_url['host'], $trackback_url['port'], $errno, $errstr, 4);
  659.     @fputs($fs, $http_request);
  660. /*
  661.     $debug_file = 'trackback.log';
  662.     $fp = fopen($debug_file, 'a');
  663.     fwrite($fp, "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n");
  664.     while(!@feof($fs)) {
  665.         fwrite($fp, @fgets($fs, 4096));
  666.     }
  667.     fwrite($fp, "\n\n");
  668.     fclose($fp);
  669. */
  670.     @fclose($fs);
  671.  
  672.     $wpdb->query("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = '$ID'");
  673.     $wpdb->query("UPDATE $wpdb->posts SET to_ping = REPLACE(to_ping, '$tb_url', '') WHERE ID = '$ID'");
  674.     return $result;
  675. }
  676.  
  677. function make_url_footnote($content) {
  678.     preg_match_all('/<a(.+?)href=\"(.+?)\"(.*?)>(.+?)<\/a>/', $content, $matches);
  679.     $j = 0;
  680.     for ($i=0; $i<count($matches[0]); $i++) {
  681.         $links_summary = (!$j) ? "\n" : $links_summary;
  682.         $j++;
  683.         $link_match = $matches[0][$i];
  684.         $link_number = '['.($i+1).']';
  685.         $link_url = $matches[2][$i];
  686.         $link_text = $matches[4][$i];
  687.         $content = str_replace($link_match, $link_text.' '.$link_number, $content);
  688.         $link_url = (strtolower(substr($link_url,0,7)) != 'http://') ? get_settings('home') . $link_url : $link_url;
  689.         $links_summary .= "\n".$link_number.' '.$link_url;
  690.     }
  691.     $content = strip_tags($content);
  692.     $content .= $links_summary;
  693.     return $content;
  694. }
  695.  
  696.  
  697. function xmlrpc_getposttitle($content) {
  698.     global $post_default_title;
  699.     if (preg_match('/<title>(.+?)<\/title>/is', $content, $matchtitle)) {
  700.         $post_title = $matchtitle[0];
  701.         $post_title = preg_replace('/<title>/si', '', $post_title);
  702.         $post_title = preg_replace('/<\/title>/si', '', $post_title);
  703.     } else {
  704.         $post_title = $post_default_title;
  705.     }
  706.     return $post_title;
  707. }
  708.     
  709. function xmlrpc_getpostcategory($content) {
  710.     global $post_default_category;
  711.     if (preg_match('/<category>(.+?)<\/category>/is', $content, $matchcat)) {
  712.         $post_category = trim($matchcat[1], ',');
  713.         $post_category = explode(',', $post_category);
  714.     } else {
  715.         $post_category = $post_default_category;
  716.     }
  717.     return $post_category;
  718. }
  719.  
  720. function xmlrpc_removepostdata($content) {
  721.     $content = preg_replace('/<title>(.+?)<\/title>/si', '', $content);
  722.     $content = preg_replace('/<category>(.+?)<\/category>/si', '', $content);
  723.     $content = trim($content);
  724.     return $content;
  725. }
  726.  
  727. function debug_fopen($filename, $mode) {
  728.     global $debug;
  729.     if ($debug == 1) {
  730.         $fp = fopen($filename, $mode);
  731.         return $fp;
  732.     } else {
  733.         return false;
  734.     }
  735. }
  736.  
  737. function debug_fwrite($fp, $string) {
  738.     global $debug;
  739.     if ($debug == 1) {
  740.         fwrite($fp, $string);
  741.     }
  742. }
  743.  
  744. function debug_fclose($fp) {
  745.     global $debug;
  746.     if ($debug == 1) {
  747.         fclose($fp);
  748.     }
  749. }
  750.  
  751. function do_enclose( $content, $post_ID ) {
  752.     global $wp_version, $wpdb;
  753.     include_once (ABSPATH . WPINC . '/class-IXR.php');
  754.  
  755.     $log = debug_fopen(ABSPATH . '/enclosures.log', 'a');
  756.     $post_links = array();
  757.     debug_fwrite($log, 'BEGIN '.date('YmdHis', time())."\n");
  758.  
  759.     $pung = get_enclosed( $post_ID );
  760.  
  761.     $ltrs = '\w';
  762.     $gunk = '/#~:.?+=&%@!\-';
  763.     $punc = '.:?\-';
  764.     $any = $ltrs . $gunk . $punc;
  765.  
  766.     preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
  767.  
  768.     debug_fwrite($log, 'Post contents:');
  769.     debug_fwrite($log, $content."\n");
  770.  
  771.     foreach($post_links_temp[0] as $link_test) :
  772.         if ( !in_array($link_test, $pung) ) : // If we haven't pung it already
  773.             $test = parse_url($link_test);
  774.             if (isset($test['query']))
  775.                 $post_links[] = $link_test;
  776.             elseif(($test['path'] != '/') && ($test['path'] != ''))
  777.                 $post_links[] = $link_test;
  778.         endif;
  779.     endforeach;
  780.  
  781.     foreach ($post_links as $url) :
  782.         if ( $url != '' && !$wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE post_id = '$post_ID' AND meta_key = 'enclosure' AND meta_value LIKE ('$url%')") ) {
  783.             if ( $headers = wp_get_http_headers( $url) ) {
  784.                 $len  = (int) $headers['content-length'];
  785.                 $type = addslashes( $headers['content-type'] );
  786.                 $allowed_types = array( 'video', 'audio' );
  787.                 if( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
  788.                     $meta_value = "$url\n$len\n$type\n";
  789.                     $wpdb->query( "INSERT INTO `$wpdb->postmeta` ( `post_id` , `meta_key` , `meta_value` )
  790.                     VALUES ( '$post_ID', 'enclosure' , '$meta_value')" );
  791.                 }
  792.             }
  793.         }
  794.     endforeach;
  795. }
  796.  
  797. function wp_get_http_headers( $url ) {
  798.     set_time_limit( 60 ); 
  799.     $parts = parse_url( $url );
  800.     $file  = $parts['path'] . ($parts['query'] ? '?'.$parts['query'] : '');
  801.     $host  = $parts['host'];
  802.     if ( !isset( $parts['port'] ) )
  803.         $parts['port'] = 80;
  804.  
  805.     $head = "HEAD $file HTTP/1.1\r\nHOST: $host\r\n\r\n";
  806.  
  807.     $fp = @fsockopen($host, $parts['port'], $err_num, $err_msg, 3);
  808.     if ( !$fp )
  809.         return false;
  810.  
  811.     $response = '';
  812.     fputs( $fp, $head );
  813.     while ( !feof( $fp ) && strpos( $response, "\r\n\r\n" ) == false )
  814.         $response .= fgets( $fp, 2048 );
  815.     fclose( $fp );
  816.     preg_match_all('/(.*?): (.*)\r/', $response, $matches);
  817.     $count = count($matches[1]);
  818.     for ( $i = 0; $i < $count; $i++) {
  819.         $key = strtolower($matches[1][$i]);
  820.         $headers["$key"] = $matches[2][$i];
  821.     }
  822.  
  823.     preg_match('/.*([0-9]{3}).*/', $response, $return);
  824.     $headers['response'] = $return[1]; // HTTP response code eg 204, 200, 404
  825.     return $headers;
  826. }
  827.  
  828. // Deprecated.  Use the new post loop.
  829. function start_wp() {
  830.     global $wp_query, $post;
  831.  
  832.     // Since the old style loop is being used, advance the query iterator here.
  833.     $wp_query->next_post();
  834.  
  835.     setup_postdata($post);
  836. }
  837.  
  838. // Setup global post data.
  839. function setup_postdata($post) {
  840.   global $id, $postdata, $authordata, $day, $page, $pages, $multipage, $more, $numpages, $wp_query;
  841.     global $pagenow;
  842.  
  843.     $id = $post->ID;
  844.  
  845.     $authordata = get_userdata($post->post_author);
  846.  
  847.     $day = mysql2date('d.m.y', $post->post_date);
  848.     $currentmonth = mysql2date('m', $post->post_date);
  849.     $numpages = 1;
  850.     $page = get_query_var('page');
  851.     if (!$page)
  852.         $page = 1;
  853.     if (is_single() || is_page())
  854.         $more = 1;
  855.     $content = $post->post_content;
  856.     if (preg_match('/<!--nextpage-->/', $content)) {
  857.         if ($page > 1)
  858.             $more = 1;
  859.         $multipage = 1;
  860.         $content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
  861.         $content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
  862.         $content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
  863.         $pages = explode('<!--nextpage-->', $content);
  864.         $numpages = count($pages);
  865.     } else {
  866.         $pages[0] = $post->post_content;
  867.         $multipage = 0;
  868.     }
  869.     return true;
  870. }
  871.  
  872. function is_new_day() {
  873.     global $day, $previousday;
  874.     if ($day != $previousday) {
  875.         return(1);
  876.     } else {
  877.         return(0);
  878.     }
  879. }
  880.  
  881. // Filters: these are the core of WP's plugin architecture
  882.  
  883. function merge_filters($tag) {
  884.     global $wp_filter;
  885.     if (isset($wp_filter['all'])) {
  886.         foreach ($wp_filter['all'] as $priority => $functions) {
  887.             if (isset($wp_filter[$tag][$priority]))
  888.                 $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], $wp_filter[$tag][$priority]);
  889.             else
  890.                 $wp_filter[$tag][$priority] = array_merge($wp_filter['all'][$priority], array());
  891.             $wp_filter[$tag][$priority] = array_unique($wp_filter[$tag][$priority]);
  892.         }
  893.     }
  894.  
  895.     if ( isset($wp_filter[$tag]) )
  896.         ksort( $wp_filter[$tag] );
  897. }
  898.  
  899. function apply_filters($tag, $string) {
  900.     global $wp_filter;
  901.     
  902.     $args = array_slice(func_get_args(), 2);
  903.  
  904.     merge_filters($tag);
  905.     
  906.     if (!isset($wp_filter[$tag])) {
  907.         return $string;
  908.     }
  909.     foreach ($wp_filter[$tag] as $priority => $functions) {
  910.         if (!is_null($functions)) {
  911.             foreach($functions as $function) {
  912.  
  913.                 $all_args = array_merge(array($string), $args);
  914.                 $function_name = $function['function'];
  915.                 $accepted_args = $function['accepted_args'];
  916.  
  917.                 if($accepted_args == 1) {
  918.                     $the_args = array($string);
  919.                 } elseif ($accepted_args > 1) {
  920.                     $the_args = array_slice($all_args, 0, $accepted_args);
  921.                 } elseif($accepted_args == 0) {
  922.                     $the_args = NULL;
  923.                 } else {
  924.                     $the_args = $all_args;
  925.                 }
  926.  
  927.                 $string = call_user_func_array($function_name, $the_args);
  928.             }
  929.         }
  930.     }
  931.     return $string;
  932. }
  933.  
  934. function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
  935.     global $wp_filter;
  936.  
  937.     // check that we don't already have the same filter at the same priority
  938.     if (isset($wp_filter[$tag]["$priority"])) {
  939.         foreach($wp_filter[$tag]["$priority"] as $filter) {
  940.             // uncomment if we want to match function AND accepted_args
  941.             //if ($filter == array($function, $accepted_args)) {
  942.             if ($filter['function'] == $function_to_add) {
  943.                 return true;
  944.             }
  945.         }
  946.     }
  947.  
  948.     // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]']
  949.     $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args);
  950.     return true;
  951. }
  952.  
  953. function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
  954.     global $wp_filter;
  955.  
  956.     // rebuild the list of filters
  957.     if (isset($wp_filter[$tag]["$priority"])) {
  958.         foreach($wp_filter[$tag]["$priority"] as $filter) {
  959.             if ($filter['function'] != $function_to_remove) {
  960.                 $new_function_list[] = $filter;
  961.             }
  962.         }
  963.         $wp_filter[$tag]["$priority"] = $new_function_list;
  964.     }
  965.     return true;
  966. }
  967.  
  968. // The *_action functions are just aliases for the *_filter functions, they take special strings instead of generic content
  969.  
  970. function do_action($tag, $arg = '') {
  971.     global $wp_filter;
  972.     $extra_args = array_slice(func_get_args(), 2);
  973.      if ( is_array($arg) )
  974.          $args = array_merge($arg, $extra_args);
  975.     else
  976.         $args = array_merge(array($arg), $extra_args);
  977.     
  978.     merge_filters($tag);
  979.     
  980.     if (!isset($wp_filter[$tag])) {
  981.         return;
  982.     }
  983.     foreach ($wp_filter[$tag] as $priority => $functions) {
  984.         if (!is_null($functions)) {
  985.             foreach($functions as $function) {
  986.  
  987.                 $function_name = $function['function'];
  988.                 $accepted_args = $function['accepted_args'];
  989.  
  990.                 if($accepted_args == 1) {
  991.                     if ( is_array($arg) )
  992.                         $the_args = $arg;
  993.                     else
  994.                         $the_args = array($arg);
  995.                 } elseif ($accepted_args > 1) {
  996.                     $the_args = array_slice($args, 0, $accepted_args);
  997.                 } elseif($accepted_args == 0) {
  998.                     $the_args = NULL;
  999.                 } else {
  1000.                     $the_args = $args;
  1001.                 }
  1002.  
  1003.                 $string = call_user_func_array($function_name, $the_args);
  1004.             }
  1005.         }
  1006.     }
  1007. }
  1008.  
  1009. function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
  1010.     add_filter($tag, $function_to_add, $priority, $accepted_args);
  1011. }
  1012.  
  1013. function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
  1014.     remove_filter($tag, $function_to_remove, $priority, $accepted_args);
  1015. }
  1016.  
  1017. function get_page_uri($page_id) {
  1018.     $page = get_page($page_id);
  1019.     $uri = urldecode($page->post_name);
  1020.  
  1021.     // A page cannot be it's own parent.
  1022.     if ($page->post_parent == $page->ID) {
  1023.         return $uri;
  1024.     }
  1025.     
  1026.     while ($page->post_parent != 0) {
  1027.         $page = get_page($page->post_parent);
  1028.         $uri = urldecode($page->post_name) . "/" . $uri;
  1029.     }
  1030.  
  1031.     return $uri;
  1032. }
  1033.  
  1034. function get_posts($args) {
  1035.     global $wpdb;
  1036.     parse_str($args, $r);
  1037.     if (!isset($r['numberposts'])) $r['numberposts'] = 5;
  1038.     if (!isset($r['offset'])) $r['offset'] = 0;
  1039.     if (!isset($r['category'])) $r['category'] = '';
  1040.     if (!isset($r['orderby'])) $r['orderby'] = 'post_date';
  1041.     if (!isset($r['order'])) $r['order'] = 'DESC';
  1042.  
  1043.     $now = current_time('mysql');
  1044.  
  1045.     $posts = $wpdb->get_results(
  1046.         "SELECT DISTINCT * FROM $wpdb->posts " .
  1047.         ( empty( $r['category'] ) ? "" : ", $wpdb->post2cat " ) .
  1048.         " WHERE post_date <= '$now' AND (post_status = 'publish') ".
  1049.         ( empty( $r['category'] ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $r['category']. " " ) .
  1050.         " GROUP BY $wpdb->posts.ID ORDER BY " . $r['orderby'] . " " . $r['order'] . "  LIMIT " . $r['offset'] . ',' . $r['numberposts'] );
  1051.     
  1052.     update_post_caches($posts);
  1053.     
  1054.     return $posts;
  1055. }
  1056.  
  1057. function &query_posts($query) {
  1058.     global $wp_query;
  1059.     return $wp_query->query($query);
  1060. }
  1061.  
  1062. function update_post_cache(&$posts) {
  1063.     global $post_cache;
  1064.  
  1065.     if ( !$posts )
  1066.         return;
  1067.  
  1068.     for ($i = 0; $i < count($posts); $i++) {
  1069.         $post_cache[$posts[$i]->ID] = &$posts[$i];
  1070.     }
  1071. }
  1072.  
  1073. function update_page_cache(&$pages) {
  1074.     global $page_cache;
  1075.  
  1076.     if ( !$pages )
  1077.         return;
  1078.  
  1079.     for ($i = 0; $i < count($pages); $i++) {
  1080.         $page_cache[$pages[$i]->ID] = &$pages[$i];
  1081.     }
  1082. }
  1083.  
  1084. function update_post_category_cache($post_ids) {
  1085.     global $wpdb, $category_cache, $cache_categories;
  1086.  
  1087.     if (empty($post_ids))
  1088.         return;
  1089.  
  1090.     if (is_array($post_ids))
  1091.         $post_ids = implode(',', $post_ids);
  1092.  
  1093.     $dogs = $wpdb->get_results("SELECT DISTINCT
  1094.     post_id, cat_ID FROM $wpdb->categories, $wpdb->post2cat
  1095.     WHERE category_id = cat_ID AND post_id IN ($post_ids)");
  1096.  
  1097.     if (! isset($cache_categories))
  1098.         update_category_cache();
  1099.         
  1100.     if ( !empty($dogs) ) {
  1101.         foreach ($dogs as $catt) {
  1102.             $category_cache[$catt->post_id][$catt->cat_ID] = &$cache_categories[$catt->cat_ID];
  1103.         }
  1104.     }
  1105. }
  1106.  
  1107. function update_post_caches(&$posts) {
  1108.     global $post_cache, $category_cache, $comment_count_cache, $post_meta_cache;
  1109.     global $wpdb;
  1110.     
  1111.     // No point in doing all this work if we didn't match any posts.
  1112.     if ( !$posts )
  1113.         return;
  1114.  
  1115.     // Get the categories for all the posts
  1116.     for ($i = 0; $i < count($posts); $i++) {
  1117.         $post_id_list[] = $posts[$i]->ID;
  1118.         $post_cache[$posts[$i]->ID] = &$posts[$i];
  1119.     }
  1120.  
  1121.     $post_id_list = implode(',', $post_id_list);
  1122.     
  1123.     update_post_category_cache($post_id_list);
  1124.  
  1125.     // Do the same for comment numbers
  1126.     $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
  1127.     FROM $wpdb->posts
  1128.     LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID  AND comment_approved =  '1')
  1129.     WHERE ID IN ($post_id_list)
  1130.     GROUP BY ID");
  1131.     
  1132.     if ($comment_counts) {
  1133.         foreach ($comment_counts as $comment_count)
  1134.             $comment_count_cache["$comment_count->ID"] = $comment_count->ccount;
  1135.     }
  1136.  
  1137.     // Get post-meta info
  1138.     if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta  WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) {
  1139.         // Change from flat structure to hierarchical:
  1140.         $post_meta_cache = array();
  1141.         foreach ($meta_list as $metarow) {
  1142.             $mpid = $metarow['post_id'];
  1143.             $mkey = $metarow['meta_key'];
  1144.             $mval = $metarow['meta_value'];
  1145.  
  1146.             // Force subkeys to be array type:
  1147.             if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
  1148.                 $post_meta_cache[$mpid] = array();
  1149.             if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
  1150.                 $post_meta_cache[$mpid]["$mkey"] = array();
  1151.  
  1152.             // Add a value to the current pid/key:
  1153.             $post_meta_cache[$mpid][$mkey][] = $mval;
  1154.         }
  1155.     }
  1156. }
  1157.  
  1158. function update_category_cache() {
  1159.     global $cache_categories, $wpdb;
  1160.     $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories");
  1161.     foreach ($dogs as $catt)
  1162.         $cache_categories[$catt->cat_ID] = $catt;
  1163. }
  1164.  
  1165. function update_user_cache() {
  1166.     global $cache_userdata, $wpdb;
  1167.     
  1168.     if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0") ) :
  1169.         foreach ($users as $user) :
  1170.             $cache_userdata[$user->ID] = $user;
  1171.             $cache_userdata[$user->user_login] =& $cache_userdata[$user->ID];
  1172.         endforeach;
  1173.         return true;
  1174.     else : 
  1175.         return false;
  1176.     endif;
  1177. }
  1178.  
  1179. function wp_head() {
  1180.     do_action('wp_head');
  1181. }
  1182.  
  1183. function wp_footer() {
  1184.     do_action('wp_footer');
  1185. }
  1186.  
  1187. function is_single ($post = '') {
  1188.     global $wp_query;
  1189.  
  1190.     if ( !$wp_query->is_single )
  1191.         return false;
  1192.  
  1193.     if ( empty( $post) )
  1194.         return true;
  1195.  
  1196.     $post_obj = $wp_query->get_queried_object();
  1197.  
  1198.     if ( $post == $post_obj->ID ) 
  1199.         return true;
  1200.     elseif ( $post == $post_obj->post_title ) 
  1201.         return true;
  1202.     elseif ( $post == $post_obj->post_name )
  1203.         return true;
  1204.  
  1205.     return false;
  1206. }
  1207.  
  1208. function is_page ($page = '') {
  1209.     global $wp_query;
  1210.  
  1211.     if (! $wp_query->is_page) {
  1212.         return false;
  1213.     }
  1214.  
  1215.     if (empty($page)) {
  1216.         return true;
  1217.     }
  1218.  
  1219.     $page_obj = $wp_query->get_queried_object();
  1220.         
  1221.     if ($page == $page_obj->ID) {
  1222.         return true;
  1223.     } else if ($page == $page_obj->post_title) {
  1224.         return true;
  1225.     } else if ($page == $page_obj->post_name) {
  1226.         return true;
  1227.     }
  1228.  
  1229.     return false;
  1230. }
  1231.  
  1232. function is_archive () {
  1233.     global $wp_query;
  1234.  
  1235.     return $wp_query->is_archive;
  1236. }
  1237.  
  1238. function is_date () {
  1239.     global $wp_query;
  1240.  
  1241.     return $wp_query->is_date;
  1242. }
  1243.  
  1244. function is_year () {
  1245.     global $wp_query;
  1246.  
  1247.     return $wp_query->is_year;
  1248. }
  1249.  
  1250. function is_month () {
  1251.     global $wp_query;
  1252.  
  1253.     return $wp_query->is_month;
  1254. }
  1255.  
  1256. function is_day () {
  1257.     global $wp_query;
  1258.  
  1259.     return $wp_query->is_day;
  1260. }
  1261.  
  1262. function is_time () {
  1263.     global $wp_query;
  1264.  
  1265.     return $wp_query->is_time;
  1266. }
  1267.  
  1268. function is_author ($author = '') {
  1269.     global $wp_query;
  1270.  
  1271.     if (! $wp_query->is_author) {
  1272.         return false;
  1273.     }
  1274.  
  1275.     if (empty($author)) {
  1276.         return true;
  1277.     }
  1278.  
  1279.     $author_obj = $wp_query->get_queried_object();
  1280.         
  1281.     if ($author == $author_obj->ID) {
  1282.         return true;
  1283.     } else if ($author == $author_obj->user_nickname) {
  1284.         return true;
  1285.     } else if ($author == $author_obj->user_nicename) {
  1286.         return true;
  1287.     }
  1288.  
  1289.     return false;
  1290. }
  1291.  
  1292. function is_category ($category = '') {
  1293.     global $wp_query;
  1294.  
  1295.     if (! $wp_query->is_category) {
  1296.         return false;
  1297.     }
  1298.  
  1299.     if (empty($category)) {
  1300.         return true;
  1301.     }
  1302.  
  1303.     $cat_obj = $wp_query->get_queried_object();
  1304.         
  1305.     if ($category == $cat_obj->cat_ID) {
  1306.         return true;
  1307.     } else if ($category == $cat_obj->cat_name) {
  1308.         return true;
  1309.     } else if ($category == $cat_obj->category_nicename) {
  1310.         return true;
  1311.     }
  1312.  
  1313.     return false;
  1314. }
  1315.  
  1316. function is_search () {
  1317.     global $wp_query;
  1318.  
  1319.     return $wp_query->is_search;
  1320. }
  1321.  
  1322. function is_feed () {
  1323.     global $wp_query;
  1324.  
  1325.     return $wp_query->is_feed;
  1326. }
  1327.  
  1328. function is_trackback () {
  1329.     global $wp_query;
  1330.  
  1331.     return $wp_query->is_trackback;
  1332. }
  1333.  
  1334. function is_admin () {
  1335.     global $wp_query;
  1336.  
  1337.     return $wp_query->is_admin;
  1338. }
  1339.  
  1340. function is_home () {
  1341.     global $wp_query;
  1342.  
  1343.     return $wp_query->is_home;
  1344. }
  1345.  
  1346. function is_404 () {
  1347.     global $wp_query;
  1348.  
  1349.     return $wp_query->is_404;
  1350. }
  1351.  
  1352. function is_comments_popup () {
  1353.     global $wp_query;
  1354.  
  1355.     return $wp_query->is_comments_popup;
  1356. }
  1357.  
  1358. function is_paged () {
  1359.     global $wp_query;
  1360.  
  1361.     return $wp_query->is_paged;
  1362. }
  1363.  
  1364. function get_query_var($var) {
  1365.   global $wp_query;
  1366.  
  1367.   return $wp_query->get($var);
  1368. }
  1369.  
  1370. function have_posts() {
  1371.     global $wp_query;
  1372.  
  1373.     return $wp_query->have_posts();
  1374. }
  1375.  
  1376. function rewind_posts() {
  1377.     global $wp_query;
  1378.  
  1379.     return $wp_query->rewind_posts();
  1380. }
  1381.  
  1382. function the_post() {
  1383.     global $wp_query;
  1384.     $wp_query->the_post();
  1385. }
  1386.  
  1387. function get_theme_root() {
  1388.     return apply_filters('theme_root', ABSPATH . "wp-content/themes");
  1389. }
  1390.  
  1391. function get_theme_root_uri() {
  1392.     return apply_filters('theme_root_uri', get_settings('siteurl') . "/wp-content/themes", get_settings('siteurl'));
  1393. }
  1394.  
  1395. function get_stylesheet() {
  1396.     return apply_filters('stylesheet', get_settings('stylesheet'));
  1397. }
  1398.  
  1399. function get_stylesheet_directory() {
  1400.     $stylesheet = get_stylesheet();
  1401.     $stylesheet_dir = get_theme_root() . "/$stylesheet";
  1402.     return apply_filters('stylesheet_directory', $stylesheet_dir, $stylesheet);
  1403. }
  1404.  
  1405. function get_stylesheet_directory_uri() {
  1406.     $stylesheet = get_stylesheet();
  1407.     $stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
  1408.     return apply_filters('stylesheet_directory_uri', $stylesheet_dir_uri, $stylesheet);
  1409. }
  1410.  
  1411. function get_stylesheet_uri() {
  1412.     $stylesheet_dir_uri = get_stylesheet_directory_uri();
  1413.     $stylesheet_uri = $stylesheet_dir_uri . "/style.css";
  1414.     return apply_filters('stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri);
  1415. }
  1416.  
  1417. function get_template() {
  1418.     return apply_filters('template', get_settings('template'));
  1419. }
  1420.  
  1421. function get_template_directory() {
  1422.     $template = get_template();
  1423.     $template_dir = get_theme_root() . "/$template";
  1424.     return apply_filters('template_directory', $template_dir, $template);
  1425. }
  1426.  
  1427. function get_template_directory_uri() {
  1428.     $template = get_template();
  1429.     $template_dir_uri = get_theme_root_uri() . "/$template";
  1430.     return apply_filters('template_directory_uri', $template_dir_uri, $template);
  1431. }
  1432.  
  1433. function get_theme_data($theme_file) {
  1434.     $theme_data = implode('', file($theme_file));
  1435.     preg_match("|Theme Name:(.*)|i", $theme_data, $theme_name);
  1436.     preg_match("|Theme URI:(.*)|i", $theme_data, $theme_uri);
  1437.     preg_match("|Description:(.*)|i", $theme_data, $description);
  1438.     preg_match("|Author:(.*)|i", $theme_data, $author_name);
  1439.     preg_match("|Author URI:(.*)|i", $theme_data, $author_uri);
  1440.     preg_match("|Template:(.*)|i", $theme_data, $template);
  1441.     if ( preg_match("|Version:(.*)|i", $theme_data, $version) )
  1442.         $version = $version[1];
  1443.     else
  1444.         $version ='';
  1445.     if ( preg_match("|Status:(.*)|i", $theme_data, $status) )
  1446.         $status = $status[1];
  1447.     else
  1448.         $status ='publish';
  1449.  
  1450.     $description = wptexturize($description[1]);
  1451.  
  1452.     $name = $theme_name[1];
  1453.     $name = trim($name);
  1454.     $theme = $name;
  1455.     if ('' != $theme_uri[1] && '' != $name) {
  1456.         $theme = '<a href="' . $theme_uri[1] . '" title="' . __('Visit theme homepage') . '">' . $theme . '</a>';
  1457.     }
  1458.  
  1459.     if ('' == $author_uri[1]) {
  1460.         $author = $author_name[1];
  1461.     } else {
  1462.         $author = '<a href="' . $author_uri[1] . '" title="' . __('Visit author homepage') . '">' . $author_name[1] . '</a>';
  1463.     }
  1464.  
  1465.     return array('Name' => $name, 'Title' => $theme, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template[1], 'Status' => $status);
  1466. }
  1467.  
  1468. function get_themes() {
  1469.     global $wp_themes;
  1470.     global $wp_broken_themes;
  1471.  
  1472.     if (isset($wp_themes)) {
  1473.         return $wp_themes;
  1474.     }
  1475.  
  1476.     $themes = array();
  1477.     $wp_broken_themes = array();
  1478.     $theme_root = get_theme_root();
  1479.     $theme_loc = str_replace(ABSPATH, '', $theme_root);
  1480.  
  1481.     // Files in wp-content/themes directory
  1482.     $themes_dir = @ dir($theme_root);
  1483.     if ($themes_dir) {
  1484.         while(($theme_dir = $themes_dir->read()) !== false) {
  1485.             if (is_dir($theme_root . '/' . $theme_dir)) {
  1486.                 if ($theme_dir{0} == '.' || $theme_dir == '..' || $theme_dir == 'CVS') {
  1487.                     continue;
  1488.                 }
  1489.                 $stylish_dir = @ dir($theme_root . '/' . $theme_dir);
  1490.                 $found_stylesheet = false;
  1491.                 while(($theme_file = $stylish_dir->read()) !== false) {
  1492.                     if ( $theme_file == 'style.css' ) {
  1493.                         $theme_files[] = $theme_dir . '/' . $theme_file;
  1494.                         $found_stylesheet = true;
  1495.                         break;
  1496.                     }
  1497.                 }
  1498.                 if (!$found_stylesheet) {
  1499.                     $wp_broken_themes[$theme_dir] = array('Name' => $theme_dir, 'Title' => $theme_dir, 'Description' => __('Stylesheet is missing.'));
  1500.                 }
  1501.             }
  1502.         }
  1503.     }
  1504.  
  1505.     if (!$themes_dir || !$theme_files) {
  1506.         return $themes;
  1507.     }
  1508.  
  1509.     sort($theme_files);
  1510.  
  1511.     foreach($theme_files as $theme_file) {
  1512.         $theme_data = get_theme_data("$theme_root/$theme_file");
  1513.       
  1514.         $name = $theme_data['Name']; 
  1515.         $title = $theme_data['Title'];
  1516.         $description = wptexturize($theme_data['Description']);
  1517.         $version = $theme_data['Version'];
  1518.         $author = $theme_data['Author'];
  1519.         $template = $theme_data['Template'];
  1520.         $stylesheet = dirname($theme_file);
  1521.  
  1522.         if (empty($name)) {
  1523.             $name = dirname($theme_file);
  1524.             $title = $name;
  1525.         }
  1526.  
  1527.         if (empty($template)) {
  1528.             if (file_exists(dirname("$theme_root/$theme_file/index.php"))) {
  1529.                 $template = dirname($theme_file);
  1530.             } else {
  1531.                 continue;
  1532.             }
  1533.         }
  1534.  
  1535.         $template = trim($template);
  1536.  
  1537.         if (! file_exists("$theme_root/$template/index.php")) {
  1538.             $wp_broken_themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => __('Template is missing.'));
  1539.             continue;
  1540.         }
  1541.         
  1542.         $stylesheet_files = array();
  1543.         $stylesheet_dir = @ dir("$theme_root/$stylesheet");
  1544.         if ($stylesheet_dir) {
  1545.             while(($file = $stylesheet_dir->read()) !== false) {
  1546.                 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file) ) 
  1547.                     $stylesheet_files[] = "$theme_loc/$stylesheet/$file";
  1548.             }
  1549.         }
  1550.  
  1551.         $template_files = array();        
  1552.         $template_dir = @ dir("$theme_root/$template");
  1553.         if ($template_dir) {
  1554.             while(($file = $template_dir->read()) !== false) {
  1555.                 if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) ) 
  1556.                     $template_files[] = "$theme_loc/$template/$file";
  1557.             }
  1558.         }
  1559.  
  1560.         $template_dir = dirname($template_files[0]);
  1561.         $stylesheet_dir = dirname($stylesheet_files[0]);
  1562.  
  1563.         if (empty($template_dir)) $template_dir = '/';
  1564.         if (empty($stylesheet_dir)) $stylesheet_dir = '/';
  1565.  
  1566.         // Check for theme name collision.  This occurs if a theme is copied to
  1567.         // a new theme directory and the theme header is not updated.  Whichever
  1568.         // theme is first keeps the name.  Subsequent themes get a suffix applied.
  1569.         // The Default and Classic themes always trump their pretenders.
  1570.         if ( isset($themes[$name]) ) {
  1571.             if ( ('WordPress Default' == $name || 'WordPress Classic' == $name) &&
  1572.                      ('default' == $stylesheet || 'classic' == $stylesheet) ) {
  1573.                 // If another theme has claimed to be one of our default themes, move
  1574.                 // them aside.
  1575.                 $suffix = $themes[$name]['Stylesheet'];
  1576.                 $new_name = "$name/$suffix";
  1577.                 $themes[$new_name] = $themes[$name];
  1578.                 $themes[$new_name]['Name'] = $new_name;
  1579.             } else {
  1580.                 $name = "$name/$stylesheet";
  1581.             }
  1582.         }
  1583.         
  1584.         $themes[$name] = array('Name' => $name, 'Title' => $title, 'Description' => $description, 'Author' => $author, 'Version' => $version, 'Template' => $template, 'Stylesheet' => $stylesheet, 'Template Files' => $template_files, 'Stylesheet Files' => $stylesheet_files, 'Template Dir' => $template_dir, 'Stylesheet Dir' => $stylesheet_dir, 'Status' => $theme_data['Status']);
  1585.     }
  1586.  
  1587.     // Resolve theme dependencies.
  1588.     $theme_names = array_keys($themes);
  1589.  
  1590.     foreach ($theme_names as $theme_name) {
  1591.         $themes[$theme_name]['Parent Theme'] = '';
  1592.         if ($themes[$theme_name]['Stylesheet'] != $themes[$theme_name]['Template']) {
  1593.             foreach ($theme_names as $parent_theme_name) {
  1594.                 if (($themes[$parent_theme_name]['Stylesheet'] == $themes[$parent_theme_name]['Template']) && ($themes[$parent_theme_name]['Template'] == $themes[$theme_name]['Template'])) {
  1595.                     $themes[$theme_name]['Parent Theme'] = $themes[$parent_theme_name]['Name'];
  1596.                     break;
  1597.                 }
  1598.             }
  1599.         }
  1600.     }
  1601.  
  1602.     $wp_themes = $themes;
  1603.  
  1604.     return $themes;
  1605. }
  1606.  
  1607. function get_theme($theme) {
  1608.     $themes = get_themes();
  1609.  
  1610.     if (array_key_exists($theme, $themes)) {
  1611.         return $themes[$theme];
  1612.     }
  1613.  
  1614.     return NULL;
  1615. }
  1616.  
  1617. function get_current_theme() {
  1618.     $themes = get_themes();
  1619.     $theme_names = array_keys($themes);
  1620.     $current_template = get_settings('template');
  1621.     $current_stylesheet = get_settings('stylesheet');
  1622.     $current_theme = 'WordPress Default';
  1623.  
  1624.     if ($themes) {
  1625.         foreach ($theme_names as $theme_name) {
  1626.             if ($themes[$theme_name]['Stylesheet'] == $current_stylesheet &&
  1627.                     $themes[$theme_name]['Template'] == $current_template) {
  1628.                 $current_theme = $themes[$theme_name]['Name'];
  1629.                 break;
  1630.             }
  1631.         }
  1632.     }
  1633.  
  1634.     return $current_theme;
  1635. }
  1636.  
  1637. function get_query_template($type) {
  1638.     $template = '';
  1639.     if ( file_exists(TEMPLATEPATH . "/{$type}.php") )
  1640.         $template = TEMPLATEPATH . "/{$type}.php";
  1641.  
  1642.     return apply_filters("{$type}_template", $template);
  1643. }
  1644.  
  1645. function get_404_template() {
  1646.     return get_query_template('404');
  1647. }
  1648.  
  1649. function get_archive_template() {
  1650.     return get_query_template('archive');
  1651. }
  1652.  
  1653. function get_author_template() {
  1654.     return get_query_template('author');
  1655. }
  1656.  
  1657. function get_category_template() {
  1658.     $template = '';
  1659.     if ( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') )
  1660.         $template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php';
  1661.     else if ( file_exists(TEMPLATEPATH . "/category.php") )
  1662.         $template = TEMPLATEPATH . "/category.php";
  1663.  
  1664.     return apply_filters('category_template', $template);
  1665. }
  1666.  
  1667. function get_date_template() {
  1668.     return get_query_template('date');
  1669. }
  1670.  
  1671. function get_home_template() {
  1672.     $template = '';
  1673.  
  1674.     if ( file_exists(TEMPLATEPATH . "/home.php") )
  1675.         $template = TEMPLATEPATH . "/home.php";
  1676.     else if ( file_exists(TEMPLATEPATH . "/index.php") )
  1677.         $template = TEMPLATEPATH . "/index.php";
  1678.  
  1679.     return apply_filters('home_template', $template);
  1680. }
  1681.  
  1682. function get_page_template() {
  1683.     global $wp_query;
  1684.  
  1685.     $id = $wp_query->post->ID;    
  1686.     $template = get_post_meta($id, '_wp_page_template', true);
  1687.  
  1688.     if ( 'default' == $template )
  1689.         $template = '';
  1690.  
  1691.     if ( ! empty($template) && file_exists(TEMPLATEPATH . "/$template") )
  1692.         $template = TEMPLATEPATH . "/$template";
  1693.     else if ( file_exists(TEMPLATEPATH .  "/page.php") )
  1694.         $template = TEMPLATEPATH .  "/page.php";
  1695.     else
  1696.         $template = '';
  1697.  
  1698.     return apply_filters('page_template', $template);
  1699. }
  1700.  
  1701. function get_paged_template() {
  1702.     return get_query_template('paged');
  1703. }
  1704.  
  1705. function get_search_template() {
  1706.     return get_query_template('search');
  1707. }
  1708.  
  1709. function get_single_template() {
  1710.     return get_query_template('single');
  1711. }
  1712.  
  1713. function get_comments_popup_template() {
  1714.     if ( file_exists( TEMPLATEPATH . '/comments-popup.php') )
  1715.         $template = TEMPLATEPATH . '/comments-popup.php';
  1716.     else
  1717.         $template = get_theme_root() . '/default/comments-popup.php';
  1718.  
  1719.     return apply_filters('comments_popup_template', $template);
  1720. }
  1721.  
  1722. // Borrowed from the PHP Manual user notes. Convert entities, while
  1723. // preserving already-encoded entities:
  1724. function htmlentities2($myHTML) {
  1725.     $translation_table=get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
  1726.     $translation_table[chr(38)] = '&';
  1727.     return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" , strtr($myHTML, $translation_table));
  1728. }
  1729.  
  1730.  
  1731. function is_plugin_page() {
  1732.     global $plugin_page;
  1733.  
  1734.     if (isset($plugin_page)) {
  1735.         return true;
  1736.     }
  1737.  
  1738.     return false;
  1739. }
  1740.  
  1741. /*
  1742. add_query_arg: Returns a modified querystring by adding
  1743. a single key & value or an associative array.
  1744. Setting a key value to emptystring removes the key.
  1745. Omitting oldquery_or_uri uses the $_SERVER value.
  1746.  
  1747. Parameters:
  1748. add_query_arg(newkey, newvalue, oldquery_or_uri) or
  1749. add_query_arg(associative_array, oldquery_or_uri)
  1750. */
  1751. function add_query_arg() {
  1752.     $ret = '';
  1753.     if(is_array(func_get_arg(0))) {
  1754.         $uri = @func_get_arg(1);
  1755.     }
  1756.     else {
  1757.         if (@func_num_args() < 3) {
  1758.             $uri = $_SERVER['REQUEST_URI'];
  1759.         } else {
  1760.             $uri = @func_get_arg(2);
  1761.         }
  1762.     }
  1763.  
  1764.     if (strstr($uri, '?')) {
  1765.         $parts = explode('?', $uri, 2);
  1766.         if (1 == count($parts)) {
  1767.             $base = '?';
  1768.             $query = $parts[0];
  1769.         }
  1770.         else {
  1771.             $base = $parts[0] . '?';
  1772.             $query = $parts[1];
  1773.         }
  1774.     }
  1775.     else if (strstr($uri, '/')) {
  1776.         $base = $uri . '?';
  1777.         $query = '';
  1778.     } else {
  1779.         $base = '';
  1780.         $query = $uri;
  1781.     }
  1782.  
  1783.     parse_str($query, $qs);
  1784.     if (is_array(func_get_arg(0))) {
  1785.         $kayvees = func_get_arg(0);
  1786.         $qs = array_merge($qs, $kayvees);
  1787.     }
  1788.     else
  1789.     {
  1790.             $qs[func_get_arg(0)] = func_get_arg(1);
  1791.     }
  1792.  
  1793.     foreach($qs as $k => $v)
  1794.     {
  1795.             if($v != '')
  1796.         {
  1797.                     if($ret != '') $ret .= '&';
  1798.                     $ret .= "$k=$v";
  1799.         }
  1800.     }
  1801.     $ret = $base . $ret;   
  1802.     return trim($ret, '?');
  1803. }
  1804.  
  1805. function remove_query_arg($key, $query) {
  1806.     add_query_arg($key, '', $query);
  1807. }
  1808.  
  1809. function load_template($file) {
  1810.     global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query,
  1811.         $wp_rewrite, $wpdb;
  1812.  
  1813.     extract($wp_query->query_vars);
  1814.  
  1815.     require_once($file);
  1816. }
  1817.  
  1818. function add_magic_quotes($array) {
  1819.     foreach ($array as $k => $v) {
  1820.         if (is_array($v)) {
  1821.             $array[$k] = add_magic_quotes($v);
  1822.         } else {
  1823.             $array[$k] = addslashes($v);
  1824.         }
  1825.     }
  1826.     return $array;
  1827. }
  1828.  
  1829. function wp_remote_fopen( $uri ) {
  1830.     if ( ini_get('allow_url_fopen') ) {
  1831.         $fp = fopen( $uri, 'r' );
  1832.         if ( !$fp )
  1833.             return false;
  1834.         $linea = '';
  1835.         while( $remote_read = fread($fp, 4096) )
  1836.             $linea .= $remote_read;
  1837.         fclose($fp);
  1838.         return $linea;        
  1839.     } else if ( function_exists('curl_init') ) {
  1840.         $handle = curl_init();
  1841.         curl_setopt ($handle, CURLOPT_URL, $uri);
  1842.         curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
  1843.         curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
  1844.         $buffer = curl_exec($handle);
  1845.         curl_close($handle);
  1846.         return $buffer;
  1847.     } else {
  1848.         return false;
  1849.     }    
  1850. }
  1851.  
  1852. ?>